package com.xmy.cultivate.controller;

import com.lkl.laop.sdk.LKLSDK;
import com.lkl.laop.sdk.request.V3CcssCounterOrderSpecialCreateRequest;
import com.lkl.laop.sdk.request.V3LabsTransPreorderRequest;
import com.lkl.laop.sdk.request.model.*;
import com.lkl.laop.sdk.utils.JsonUtils;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/lklTest")
public class LklTestController extends BaseController {

    @GetMapping("/test")
    public Result test() throws Exception {

        // 1. 配置初始化
        doInit();

        //2. 装配数据
        V3CcssCounterOrderSpecialCreateRequest req = new V3CcssCounterOrderSpecialCreateRequest();
        req.setOutOrderNo("KFPT20240517160009228901138");
        req.setMerchantNo("82229007392000A");
        //req.setTermNo("A1160237");
        req.setTotalAmount(1L);//订单金额，单位：分
        req.setOrderEfficientTime("20241227232953");//订单有效期 格式yyyyMMddHHmmss,最大支持下单时间+7天
        req.setNotifyUrl("http://xmy.nat300.top/api/lkl/lklOrder/callBack");//订单支付成功后商户接收订单通知的地址 http://xxx.xxx.com
        req.setSupportRefund(1);//是否支持退款 默认0 不支持
        req.setSupportRepeatPay(1);//是否支持“多次发起支付” 默认0 不支持
        req.setSupportCancel(1);
        //req.setCounterParam("{\"pay_mode\":\"ALIPAY\"}");//支付方式
        req.setCounterRemark("备注");//收银台备注
        //req.setGoodsMark("1");
        //req.setProductId("200809");
        req.setAgeLimit("0");//0:不限年龄；1:年龄限制
        req.setRepeatPayAutoRefund("0");//0:重复支付后不自动退货；1:重复支付后自动退货
        req.setRepeatPayNotify("0");//0:重复支付订单不通知；1:重复支付订单通知 （默认不送为0）
        req.setCloseOrderAutoRefund("0");//0:不自动退货；1:关闭订单后支付成功触发自动退货 （默认不送为0）
        req.setShopName("周诗涵-英语、数学");//网点名称

//        V3CcssCounterOrderSpecialCreateRequest.TransferField transferField = new V3CcssCounterOrderSpecialCreateRequest.TransferField();
//        transferField.setAccOutName("张三");
//        transferField.setAccOutNo("62266211223334323");
//        transferField.setAdditional("转账附言");
//        req.setTransferField(transferField);
//
//        V3CcssOrderGoodsFieldInfo v3CcssOrderGoodsFieldInfo = new V3CcssOrderGoodsFieldInfo();
//        v3CcssOrderGoodsFieldInfo.setGoodsAmt(100L);
//        v3CcssOrderGoodsFieldInfo.setGoodsNum(1);
//        v3CcssOrderGoodsFieldInfo.setGoodsPricingUnit("1");
//        v3CcssOrderGoodsFieldInfo.setGoodsName("卅");
//        v3CcssOrderGoodsFieldInfo.setTePlatformType("2");
//        v3CcssOrderGoodsFieldInfo.setTePlatformName("amazon");
//        v3CcssOrderGoodsFieldInfo.setGoodsType("11");
//        req.setGoodsField(JsonUtils.toJSONString(v3CcssOrderGoodsFieldInfo));
//
//        req.setSupportCancel(0);
//        req.setBusiTypeParam("[{\"busi_type\":\"UPCARD\",\"params\":{\"crd_flg\":\"CRDFLG_D|CRDFLG_C|CRDFLG_OTH\"}},{\"busi_type\":\"SCPAY\",\"params\":{\"pay_mode\":\"WECHAT\",\"crd_flg\":\"CRDFLG_D\"}}]");
//
//        req.setOrderInfo("测试12313131");
//        req.setSplitMark("0");
//        List<V3CcssOrderOutSplitInfo> outSplitInfos = new ArrayList<>();
//        V3CcssOrderOutSplitInfo outSplitInfo = new V3CcssOrderOutSplitInfo();
//        outSplitInfo.setMerchantNo("82229007392000A");
//        outSplitInfo.setOutSubOrderNo("8221210594300JY20230823151137008");
//        outSplitInfo.setAmount("100");
//        outSplitInfo.setTermNo("T12331234");
//        outSplitInfos.add(outSplitInfo);
//        //req.setOutSplitInfo(outSplitInfos);
//        List<String> sgnInfos = new ArrayList<>();
//        sgnInfos.add("1");
//        req.setSgnInfo(sgnInfos);



        /* 花呗分期场景 **/
//        V3CcssOrderSceneFieldInfo hbFq = new V3CcssOrderSceneFieldInfo();
//        V3CcssOrderSceneFieldInfo.HbFqSceneInfo hbFqSceneInfo = new V3CcssOrderSceneFieldInfo.HbFqSceneInfo();
//        hbFqSceneInfo.setHbFqNum("3");
//        hbFqSceneInfo.setHbFqSellerPercent("0");
//        hbFq.setSceneInfo(JsonUtils.toJSONString(hbFqSceneInfo));
//        hbFq.setOrderSceneType("HB_FQ");
//
//        /* 线上外卡场景  **/
//        V3CcssOrderSceneFieldInfo onlineWild = new V3CcssOrderSceneFieldInfo();
//        onlineWild.setOrderSceneType("ONLINE_WILDCARD");
//        V3CcssOrderSceneFieldInfo.OnlineWildcardSceneInfo onlineWildcardSceneInfo = new V3CcssOrderSceneFieldInfo.OnlineWildcardSceneInfo();
//        onlineWildcardSceneInfo.setUserId("12312312");
//        onlineWildcardSceneInfo.setUserCreateDate("20230402121212");
//        onlineWildcardSceneInfo.setRecipientStreet("Main Street");
//        onlineWildcardSceneInfo.setRecipientCity("New York");
//        onlineWildcardSceneInfo.setRecipientStateOrProvince("BT");
//        onlineWildcardSceneInfo.setRecipientCountry("USA");
//        onlineWild.setSceneInfo(JsonUtils.toJSONString(onlineWildcardSceneInfo));
//
//        V3CcssOrderSceneFieldInfo.OnlineWildcardEncryptedField onlineWildcardEncryptedField = new V3CcssOrderSceneFieldInfo.OnlineWildcardEncryptedField();
//        V3CcssOrderSceneOnlineWildcardBillAddress billAddress = new V3CcssOrderSceneOnlineWildcardBillAddress();
//        billAddress.setHouseNumberOrName("123 Main St");
//        billAddress.setStreet("Main Street");
//        billAddress.setCountry("USA");
//        billAddress.setCity("New York");
//        billAddress.setStateOrProvince("BT");
//        billAddress.setPostalCode("10001");
//        onlineWildcardEncryptedField.setBillingAddress(JsonUtils.toJSONString(billAddress));
//        onlineWildcardEncryptedField.setEmail("123124141@163.com");
//        onlineWildcardEncryptedField.setMobilePhone("+86 13531231231");
//        onlineWildcardEncryptedField.setRecipientFirstName("三");
//        onlineWildcardEncryptedField.setRecipientLastName("张");
//        onlineWild.setEncryptedField(JsonUtils.toJSONString(onlineWildcardEncryptedField));
//
//
//        /* 考拉分期场景  **/
//        V3CcssOrderSceneFieldInfo klfq = new V3CcssOrderSceneFieldInfo();
//        klfq.setOrderSceneType("KL_FQ");
//        V3CcssOrderSceneFieldInfo.KlFqSceneInfo klFqSceneInfo = new V3CcssOrderSceneFieldInfo.KlFqSceneInfo();
//        V3CcssOrderSceneFieldInfo.UnFqInfo unFqInfo = new V3CcssOrderSceneFieldInfo.UnFqInfo();
//        unFqInfo.setIsForce("01");
//        unFqInfo.setUnFqNum("6");
//        unFqInfo.setUnFqSellerPercent("0");
//        klFqSceneInfo.setUnfq(JsonUtils.toJSONString(unFqInfo));
//        V3CcssOrderSceneFieldInfo.HbFqSceneInfo hbFqSceneInfo2 = new V3CcssOrderSceneFieldInfo.HbFqSceneInfo();
//        hbFqSceneInfo2.setHbFqNum("3");
//        hbFqSceneInfo2.setHbFqSellerPercent("0");
//        klFqSceneInfo.setHbfq(JsonUtils.toJSONString(hbFqSceneInfo2));
//
//        klfq.setSceneInfo(JsonUtils.toJSONString(klFqSceneInfo));
//
//        V3CcssOrderSceneFieldInfo.KlFqEncryptedField klFqEncryptedField = new V3CcssOrderSceneFieldInfo.KlFqEncryptedField();
//        V3CcssOrderSceneKlFqUnFqInfo klFqUnFqInfo = new V3CcssOrderSceneKlFqUnFqInfo();
//        klFqUnFqInfo.setCardNo("623622656535665656");
//        klFqUnFqInfo.setBankNm("招商银行");
//        klFqEncryptedField.setUnfq(JsonUtils.toJSONString(klFqUnFqInfo));
//
//        klfq.setEncryptedField(JsonUtils.toJSONString(klFqEncryptedField));
//
//
//        //req.setOrderSceneField(info);
//        req.setOrderSceneField(klfq);

        //System.out.println(JsonUtils.toJSONString(req));

        //3. 发送请求
        String response = LKLSDK.httpPost(req);

        //4. 响应
        System.out.println(response);


        //String filePath = this.getClass().getClassLoader().getResource("lkl-apigw-v2.cer").getPath();

        //可以看到上面读取到路径的中文被URLEncoder编码了，所以需要先用URLDecoder解码一下，恢复中文
        //filePath = URLDecoder.decode(filePath, "UTF-8");

        return new Result(Code.OK,response,"");

    }
}
